using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using ARF.Web.Fields;
using Microsoft.SharePoint;
using System.Web;
using System.Web.UI;
using System.ComponentModel;

namespace ARF.Web.Controls
{
    [ToolboxData("<{0}:VisibilityPanel runat=\"server\" />"), ToolboxItemFilter("ARF")]
    public class VisibilityPanel : ARFPanel
    {
        protected override void OnLoad(EventArgs e)
        {
            if (string.IsNullOrEmpty(ID)) throw new Exception("VisibilityPanel ID not set");

            if (Page.IsPostBack && ARFProperties.Current.IsEditingPostback)
            {
                ARFProperties.Current.SetProperty("Visible: " + ID, !(HttpContext.Current.Request.Form["visibility_" + ID] == null));
            }

            base.OnLoad(e);
        }

        public bool IsVisible
        {
            get
            {
                return ARFProperties.Current.GetBoolProperty("Visible: " + ID, true);
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (SPContext.Current.FormContext.FormMode == Microsoft.SharePoint.WebControls.SPControlMode.Display)
            {
                if (IsVisible) base.Render(writer);
            }
            else
            {
                writer.Write("<div style='border:1px solid black'><div>Visible:<input type='checkbox' " + ((IsVisible) ? " checked='checked' " : "") + " id='visibility_" + ID + "' name='visibility_" + ID + "' /></div>");
                base.RenderChildren(writer);
                writer.Write("</div>");
            }
        }
    }
}
